package com.example.experiment4

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    var name = "--"
    var gender = "--"
    var studentId = "--"
    var phone = "--"
    var email = "--"
    var hobby = "--"
    var highScore = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        updateUI()

        // 修改个人信息按钮
        findViewById<Button>(R.id.btn_modify_info).setOnClickListener {
            val intent = Intent(this, PersonInformationActivity::class.java)
            intent.putExtra("name", name)
            intent.putExtra("gender", gender)
            intent.putExtra("studentId", studentId)
            intent.putExtra("phone", phone)
            intent.putExtra("email", email)
            intent.putExtra("hobby", hobby)
            startActivityForResult(intent, 1)
        }

        // 翻牌游戏按钮
        findViewById<Button>(R.id.btn_card_game).setOnClickListener {
            val intent = Intent(this, CardGameActivity::class.java)
            intent.putExtra("highScore", highScore)
            startActivityForResult(intent, 2)
        }
    }

    private fun updateUI() {
        findViewById<TextView>(R.id.tv_name).text = name
        findViewById<TextView>(R.id.tv_gender).text = gender
        findViewById<TextView>(R.id.tv_student_id).text = studentId
        findViewById<TextView>(R.id.tv_phone).text = phone
        findViewById<TextView>(R.id.tv_email).text = email
        findViewById<TextView>(R.id.tv_hobby).text = hobby
        findViewById<TextView>(R.id.tv_high_score).text = highScore.toString()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                1 -> {
                    name = data?.getStringExtra("name") ?: "--"
                    gender = data?.getStringExtra("gender") ?: "--"
                    studentId = data?.getStringExtra("studentId") ?: "--"
                    phone = data?.getStringExtra("phone") ?: "--"
                    email = data?.getStringExtra("email") ?: "--"
                    hobby = data?.getStringExtra("hobby") ?: "--"
                    updateUI()
                }
                2 -> {
                    highScore = data?.getIntExtra("highScore", highScore) ?: highScore
                    updateUI()
                }
            }
        }
    }
}